﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace MSDNWebcast.Views
{
    /// <summary>
    /// Interaction logic for VideoPlayer.xaml
    /// </summary>
    public partial class VideoPlayer : UserControl
    {

        private DispatcherTimer m_Timer;

        private bool m_IsUserUpdatingPosition = false;

        public VideoPlayer()
        {
            InitializeComponent();

            this.btnPause.Visibility = Visibility.Collapsed;
            m_Timer = new DispatcherTimer(DispatcherPriority.Background, mediaElement.Dispatcher);
            m_Timer.Interval = new TimeSpan(0, 0, 0, 1, 0);
            m_Timer.Tick += m_Timer_Tick;
            m_Timer.Start();
            this.mediaElement.Loaded += mediaElement_Loaded;
            this.mediaElement.MediaEnded += mediaElement_MediaEnded;
        }

        /// <summary>
        /// 视频结束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void mediaElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            if (this.mediaElement.Source != null)
            {
                this.mediaElement.Position = new TimeSpan(0, 0, 0, 0, 1);
                this.mediaElement.Source = null;
                //btnPause_Click(null, null);
                this.mediaElement.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        
        /// <summary>
        /// 设定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void m_Timer_Tick(object sender, EventArgs e)
        {
            if (mediaElement != null && mediaElement.IsLoaded && !m_IsUserUpdatingPosition)
            {
                this.ClockSlider.Value = mediaElement.Position.TotalMilliseconds;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void mediaElement_Loaded(object sender, RoutedEventArgs e)
        {
            mediaElement.LoadedBehavior = MediaState.Manual;
            mediaElement.UnloadedBehavior = MediaState.Manual;
            btnPaly_Click(null, null);
        }

        /// <summary>
        /// 播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPaly_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Play();
            this.mediaElement.Visibility = System.Windows.Visibility.Visible;
            this.VieoPlayer.Visibility = Visibility.Visible;
            this.btnPaly.Visibility = Visibility.Collapsed;
            this.btnPause.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Pause();
            this.btnPaly.Visibility = Visibility.Visible;
            this.btnPause.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// 静音/取消静音
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSound_Click(object sender, RoutedEventArgs e)
        {
            if (this.imgSound.Visibility == Visibility.Visible)
            {
                this.mediaElement.Volume = 0;
                this.imgSound.Visibility = Visibility.Collapsed;
                this.imgSoundMute.Visibility = Visibility.Visible;
            }
            else
            {
                this.mediaElement.Volume = 1;
                this.imgSound.Visibility = Visibility.Visible;
                this.imgSoundMute.Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 设定进度条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            ClockSlider.Maximum = mediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
        }

        /// <summary>
        /// 点击进度条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClockSlider_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            m_IsUserUpdatingPosition = true;
        }

        /// <summary>
        /// 点击进度条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClockSlider_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (mediaElement != null && mediaElement.NaturalDuration.HasTimeSpan && this.m_IsUserUpdatingPosition)
                mediaElement.Position = new TimeSpan(0, 0, 0, 0, (int)ClockSlider.Value);
            m_IsUserUpdatingPosition = false;
        }
    }
}
